<?php
class Feed extends TagsAbstract {
	/**
	 * 是否是封闭的标签
	 * @var unknown_type
	 */
	static $TAG_CLOSED = false;

	public $config = array();

	public function __construct(){

	}
	public function getTagStatus(){
		return self::$TAG_CLOSED;
	}
	/**
	 * 返回模板文件路径
	 */
	public function getTemplateFile($tpl = "") {
		//返回需要渲染的模板
		$file = $this->attr ['style'];
		if(!empty($tpl)){
			$file = $tpl;
		}
		return dirname(__FILE__).'/feed/'.$file.'.html';
	}

	/**
	 * 设置使用哪个Widget,暂时无用。。
	 */
	public function setWidget() {
		return 'test';
	}

	/**
	 * 这里返回的是模板中需要渲染的变量
	 */
	public function replace($checkCache) {
		$limit  = isset($this->attr['limit']) ? intval($this->attr['limit']) : 20;
		!isset($this->attr['words']) && $this->attr['words'] = 10;

		//根据数据源组合map
		$map['display'] = 0; //显示级别：所有

		//临时修改
		if(!empty($this->attr['source']) && $this->attr['source'] == 'record') {
			$map['type'] = 'space_record';
		}

		if(!empty($this->attr['source'])) {
				
			//临时修改
			$source = explode(',', $this->attr['source']);
			if(in_array('record', $source)) {
				$this->attr['source'] .= ',space';
			}

			$map['appname'] = array( 'in', explode(',', $this->attr['source']) );
		}
		if(!empty($this->attr['group'])) {
			$map['fid'] = array('in', $this->attr['group']);
		}
		if(!empty($this->attr['user'])) {
			$umodel = M('user');
			$users  = explode(',', $this->attr['user']);
			$uname = array();
			foreach($users as $k => $v) {
				if(!is_numeric($v)) {
					$uname[] = $v;
					unset($users[$k]);
				}
			}
			if(!empty($uname)){
				$tempMap['uname'] = array('in',$uname);
				$tempUser = $umodel->where($tempMap)->field('uid')->findAll();
				foreach($tempUser as $value){
					$users[] = $value['uid'];
				}
			}
			$users = array_unique($users);
			$map['uid'] = array('in', $users);
		}

		//获取动态详情
		$dao = service('Feed');
		$res = $dao->get($map, $limit);

		$var['title'] = t($this->attr['title']);
		$var['list'] = $res['data'];
		$var['html'] = $res['html'];
		$var['attr'] = $this->attr;

		return $var;
	}

	/** ********************* 自定义函数 ********************* **/

	//将二维数组格式化为一维数组
	protected function _format($arr, $field) {
		$res = array();
		foreach($arr as $v) {
			$res[] = $v[$field];
		}
		return $res;
	}
}
?>